package com.zadetk.helpyou.activities;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.goyourfly.multi_picture.ImageLoader;
import com.goyourfly.multi_picture.MultiPictureView;
import com.zadetk.helpyou.R;
import com.zadetk.helpyou.base.AbsActivity;
import com.zadetk.helpyou.utils.LogUtil;
import com.zadetk.helpyou.utils.ToastUtil;
import com.zadetk.helpyou.view.NoScrollView;
import com.zadetk.helpyou.view.dialog.BaseNiceDialog;
import com.zadetk.helpyou.view.dialog.NiceDialog;
import com.zadetk.helpyou.view.dialog.ViewConvertListener;
import com.zadetk.helpyou.view.dialog.ViewHolder;

import java.io.File;
import java.util.ArrayList;

import me.nereo.multi_image_selector.MultiImageSelector;

/**
 * 4-3-3服务详情页面
 */
public class PublishServiceActivity extends AbsActivity implements View.OnClickListener {
    private EditText etTitle;
    private TextView tvTitleNum;
    private EditText etContent;
    private TextView tvContentNum;
    private NoScrollView gvTag;
    private MultiPictureView multipleImage;
    private TextView tvFulltime;
    private TextView tvParttime;
    private TextView tvChooseMethod;
    private EditText etPrice;
    private TextView tvPublish;
    private ArrayList<String> list = new ArrayList<>();


    @Override
    public int setView() {
        return R.layout.activity_publish_service;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initViews();
        initLogic();
    }

    private void initLogic() {
        MultiPictureView.setImageLoader(new ImageLoader() {
            @Override
            public void loadImage(ImageView image, Uri uri) {
                Glide.with(PublishServiceActivity.this)
                        .load(uri)
                        .placeholder(R.drawable.placeholder)
                        .error(R.drawable.placeholder)
                        .into(image);
            }
        });
//        list.add("");
//        list.add("");
//        list.add("");
//        list.add("");
//        list.add("");
//        gvTag.setAdapter(new MyAdapter<String>(list, R.layout.tag_text) {
//            @Override
//            public void bindView(ViewHolder holder, String obj) {
//
//            }
//        });
        tvChooseMethod.setOnClickListener(this);

        tvPublish.setOnClickListener(this);
        tvParttime.setOnClickListener(this);
        tvFulltime.setOnClickListener(this);
        multipleImage.setAddClickCallback(new MultiPictureView.AddClickCallback() {
            @Override
            public void onAddClick(View view) {
                performCodeWithPermission("需要相机权限", new PermissionCallback() {
                    @Override
                    public void hasPermission() {
                        MultiImageSelector.create().showCamera(true).multi().count(9).start(PublishServiceActivity.this, 200);
                    }

                    @Override
                    public void noPermission() {

                    }
                }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA});
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 200) {
            ArrayList<String> list = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
            LogUtil.d(list.get(0));
            ToastUtil.showToast(list.get(0));
            for (String path : list) {
                File file = new File(path);
                Uri uri = Uri.fromFile(file);
                multipleImage.addItem(uri);
            }
        }
    }

    private void initViews() {
        setTopTitle("服务详情");
        etTitle = (EditText) findViewById(R.id.et_title);
        tvTitleNum = (TextView) findViewById(R.id.tv_title_num);
        etContent = (EditText) findViewById(R.id.et_content);
        tvContentNum = (TextView) findViewById(R.id.tv_content_num);
        gvTag = (NoScrollView) findViewById(R.id.gv_tag);
        multipleImage = (MultiPictureView) findViewById(R.id.multiple_image);
        tvFulltime = (TextView) findViewById(R.id.tv_fulltime);
        tvParttime = (TextView) findViewById(R.id.tv_parttime);
        tvChooseMethod = findViewById(R.id.tv_choose_method);
        etPrice = (EditText) findViewById(R.id.et_price);
        tvPublish = (TextView) findViewById(R.id.tv_publish);

        etTitle.addTextChangedListener(new TextWatcher() {

            private CharSequence etTitletemp;
            private int editTitletStart;
            private int editTitletentEnd;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                etTitletemp = charSequence;
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTitletStart = etTitle.getSelectionStart();
                editTitletentEnd = etTitle.getSelectionEnd();
                tvTitleNum.setText(etTitletemp.length() + "/15");
                if (etTitletemp.length() > 15) {
                    ToastUtil.showToast("你输入的字数已经超过了限制！");
                    editable.delete(editTitletStart - 1, editTitletentEnd);
                    int tempSelection = editTitletStart;
                    etTitle.setText(editable);
                    etTitle.setSelection(tempSelection);
                }
            }
        });

        etContent.addTextChangedListener(new TextWatcher() {//服务详情输入内容判断
            private CharSequence Contenttemp;
            private int editContentStart;
            private int editetContentEnd;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Contenttemp = charSequence;
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                editContentStart = etContent.getSelectionStart();
                editetContentEnd = etContent.getSelectionEnd();
                tvContentNum.setText(Contenttemp.length() + "/500");
                if (Contenttemp.length() > 500) {
                    ToastUtil.showToast("你输入的字数已经超过了限制！");
                    editable.delete(editContentStart - 1, editetContentEnd);
                    int tempSelection = editContentStart;
                    etContent.setText(editable);
                    etContent.setSelection(tempSelection);
                }

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_choose_method:
                showChoose();
                break;
            case R.id.tv_publish:
                break;
            case R.id.tv_parttime:
                break;
            case R.id.tv_fulltime:
                break;
        }
    }

    private void showChoose() {
        NiceDialog niceDialog = NiceDialog.init();
        niceDialog.setLayoutId(R.layout.pay_choose).setConvertListener(new ViewConvertListener() {
            @Override
            protected void convertView(ViewHolder var1, BaseNiceDialog var2) {

            }
        }).setShowBottom(true).show(getSupportFragmentManager());
    }
}
